﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstituteManager.Data;
using System.Data;

namespace InstituteManager.Bussiness
{
    public class ExhibitionDetailBL
    {
        public static string errorMsg = "";
        public static List<tbl_ExhibitionDetail> getByAll()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_ExhibitionDetail> exhi = InsMana.tbl_ExhibitionDetail.ToList();
            return exhi;
        }

        public static tbl_ExhibitionDetail getById(int Id)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_ExhibitionDetail.First(temp => temp.ExhibitionDetailId == Id);
        }

        public static bool AddNew(int ExhibitionId, int PostingId)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_ExhibitionDetail obj = new tbl_ExhibitionDetail();
                obj.tbl_Exhibition = InsMana.tbl_Exhibition.First(temp => temp.ExhibitionId == ExhibitionId);
                obj.tbl_Posting = InsMana.tbl_Posting.First(temp => temp.PostingId == PostingId);
                if ((from sc in InsMana.tbl_ExhibitionDetail.Include("tbl_Exhibition").Include("tbl_Posting") where sc.tbl_Exhibition.ExhibitionId == ExhibitionId && sc.tbl_Posting.PostingId == PostingId select sc).Count() > 0)
                {
                    errorMsg = "You already sent this posting to Exhibition !";
                    return false;
                }
                InsMana.AddTotbl_ExhibitionDetail(obj);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Update(int ExhibitionDetailId ,int ExhibitionId, int PostingId)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_ExhibitionDetail obj = InsMana.tbl_ExhibitionDetail.First(temp => temp.ExhibitionDetailId == ExhibitionDetailId);
                obj.tbl_Exhibition = InsMana.tbl_Exhibition.First(temp => temp.ExhibitionId == ExhibitionId);
                obj.tbl_Posting = InsMana.tbl_Posting.First(temp => temp.PostingId == PostingId);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Delete(int Id)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                InsMana.DeleteObject(InsMana.tbl_ExhibitionDetail.First(temp => temp.ExhibitionDetailId == Id));
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }
    }
}
